package com.neu.yth.nepBackend.gateway.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @ClassName GemmeCorsConfig
 * @Description
 * @Author YTH
 * @Date 2024-07-11 19:06
 */
@Configuration
@Slf4j
public class CorsConfig {
    /**
     * 全局跨域配置
     *
     * @return
     */
    @Bean
    public CorsWebFilter corsWebFilter() {

        log.info("跨域配置成功");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //1、配置跨域
        corsConfiguration.addAllowedHeader("*");
        //请求头
        corsConfiguration.addAllowedMethod("*");
        //请求方式
        corsConfiguration.addAllowedOriginPattern("http://localhost:*");
        //请求来源
        corsConfiguration.setAllowCredentials(true);
        //cookie请求
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}
